import NProgress from "nprogress";
const BaseURL = "http://www.pudge.wang:3080/api";
const http = {
  get(url, options) {
    // 发送数据 url是/rated/list  params是{a: 3,b: 4}   ->    /rated/list?a=3&b=4
    if (options) {
      url += "?";
      let arr = [];
      for (const key in options) {
        arr.push(key + "=" + options[key]);
      }
      url += arr.join("&");
    }
    return new Promise((resolve) => {
      NProgress.start();
      return fetch(BaseURL + url)
        .then((response) => response.json())
        .then((res) => {
          NProgress.done();
          if (res.status === 0) {
            resolve(res);
          }
        });
    });
  },
  /* fetch的post 请求
      fetch("url地址"，{
        body:JSON.stringfy("data"),
        headers: {"content-type": "application/json",},
        method: "POST"})
   */
  post(url, data) {
    return new Promise((resolve) => {
      NProgress.start();
      return fetch(BaseURL + url, {
        body: JSON.stringify(data),
        headers: {
          "content-type": "application/json",
        },
        method: "POST",
      })
        .then((response) => response.json())
        .then((res) => {
          NProgress.done();
          if (res.status === 0) {
            resolve(res);
          }
        });
    });
  },
};
export default http;
